<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<title></title>
	<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
	<script language="javascript" src="/js/json2.js"></script>
	<script language="javascript" src="/js/litpen_ajax.js"></script>
	
	<style type="text/css" >
		.fencheng{
			background-color:#ACD3F7;
		}
		.fencheng2{
			background-color:#eee;
		}
	</style>
    <script language="javascript" type="text/javascript">
    //<![CDATA[
    	var store = null;
    	var standByPackage = false;
    	var standByStore = false;
    	var sendRequest = false;
    	var timeId;
    	function initPage(){
	    	loadStore();
    		timeId = setInterval("bindStore()",40);
    	}
    	
    	function loadPackage(){
    		var url = "/mycndesign/showPackage.do";
    		var check_ajax = new Ajax(url,null,true,loadPackageCallBack,"get");
    		check_ajax.send();
    	}
    	
    	function loadStore(){
    		var url = "/mycndesign/showStore.do";
    		var check_ajax = new Ajax(url,null,true,loadStoreCallBack,"get");
    		check_ajax.send();
    	}
    	
    	function addStore(){
			var form = document.forms['addStoreForm'];
			var check_ajax = new Ajax(form.action,formToRequestString(form),true,addStoreCallback,"post");
			check_ajax.send();
		}
		
		function addPackage(){
			var form = document.forms['addPackageForm'];
			var check_ajax = new Ajax(form.action,formToRequestString(form),true,addPackageCallback,"post");
			check_ajax.send();
		}
		
		function deleteStore(id,name){
			if (confirm("确定删除？" + name)){
				var url = "/mycndesign/deleteStore.do?storeId=" + id;
	    		var check_ajax = new Ajax(url,null,true,deleteStoreCallBack,"get");
	    		check_ajax.send();
			}
		}
		
    	function deletePackage(id,name){
			if (confirm("确定删除？" + name)){
				var url = "/mycndesign/deletePackage.do?pid=" + id;
	    		var check_ajax = new Ajax(url,null,true,deletePackageCallBack,"get");
	    		check_ajax.send();
			}
		}
		
		function updatePackageOP(){
			var form = document.forms['updatePackage'];
			var check_ajax = new Ajax(form.action,formToRequestString(form),true,updatePackageOPCallback,"post");
			check_ajax.send();
		}
		
		function updateStoreOP(){
			var form = document.forms['updateStore'];
			var check_ajax = new Ajax(form.action,formToRequestString(form),true,updateStoreOPCallback,"post");
			check_ajax.send();
		}
		
    	function loadPackageCallBack(){
    		var result = arguments[0];
				
			if (check_error(result)){
				var error = result.split(":");
				alert(error[1]);
				return;
			}
			
			//开始获取数据
			var myData = JSON.parse(result,null);
			
			var oTBody = document.getElementById("packageData");
				while (oTBody.hasChildNodes()){
					oTBody.removeChild(oTBody.firstChild);
				}
				
				for (var i = 0; i< myData.packages.length;i++){
					//Tr格式化
					var oRow = document.createElement("TR");
					oRow.setAttribute("align","center");
				
					if (i % 2 ==1)
						oRow.className = "fencheng";
					else
						oRow.className = "fencheng2";
					oTBody.appendChild(oRow);
					
					//td初始化
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].productName;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].crqty;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].mpoint;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].amount;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].vipType == 1 ? "是":"否";
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					for (var j = 0; j<store.length;j++){
						if (myData.packages[i].accountId == store[j][0]){
							oCell.innerText = store[j][1];	
							break;
						}
					}
					if (oCell.innerText == ""){
						oCell.innerText = "没有绑定仓库";
					}
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].i1;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerText = myData.packages[i].createTime;
					oRow.appendChild(oCell);
					
					oCell = document.createElement("TD");
					oCell.innerHTML = "<input type='button' onclick='deletePackage(" + myData.packages[i].id + ",\"" + myData.packages[i].productName + "\");' value='删除'><input type='button' onclick='update(this," + myData.packages[i].id + ");' value='修改'>";
				
					oRow.appendChild(oCell);
				}
			standByPackage = true;
    	}
    	
    	function loadStoreCallBack(){
    		var result = arguments[0];
				
			if (check_error(result)){
				var error = result.split(":");
				alert(error[1]);
				return;
			}
			
			//开始获取数据
			var myData = JSON.parse(result,null);
			//开始获取数据
			var myData = JSON.parse(result,null);
			
			var oTBody = document.getElementById("storeDate");
			while (oTBody.hasChildNodes()){
				oTBody.removeChild(oTBody.firstChild);
			}
			store = new Array();
			
			for (var i = 0; i< myData.stores.length;i++){
				store.push(new Array(myData.stores[i].accountId,myData.stores[i].accountName));
				//Tr格式化
				var oRow = document.createElement("TR");
				oRow.setAttribute("align","center");
				if (i % 2 ==1)
					oRow.className = "fencheng";
				else
					oRow.className = "fencheng2";
				oTBody.appendChild(oRow);
				
				//td初始化
				oCell = document.createElement("TD");
				oCell.innerText = myData.stores[i].accountName;
				oRow.appendChild(oCell);
				oCell = document.createElement("TD");
				oCell.innerText = myData.stores[i].password;
				oRow.appendChild(oCell);
				
				oCell = document.createElement("TD");
				oCell.innerText = myData.stores[i].createTime;
				oRow.appendChild(oCell);
				
				oCell = document.createElement("TD");
				oCell.innerHTML = "<input type='button' onclick='deleteStore(" + myData.stores[i].accountId + ",\"" + myData.stores[i].accountName + "\");' value='删除'><input type='button' onclick='updateStore2(this," + myData.stores[i].accountId + ");' value='修改'>";
				oRow.appendChild(oCell);
				
				
			}
			var form = document.forms['addPackageForm'];
			var form2 = document.forms('updatePackage');
			for(var j=form.accountId.options.length; j>=0; j--) {
				form.accountId.remove(j);
				
        	}
        	for(var j=form2.accountId.options.length; j>=0; j--) {
				form2.accountId.remove(j);
				
        	}
        	for (var i=0;i<store.length;i++){
        		var optional=document.createElement("option");
          		optional.value=store[i][0];
          		optional.text=store[i][1];
          		var optional2=document.createElement("option");
          		optional2.value=store[i][0];
          		optional2.text=store[i][1];
          		
				form.accountId.add(optional);
				form2.accountId.add(optional2);
        	}
			
			standByStore = true;
    	}
    	
    	function addStoreCallback(){
    		var form = document.forms("addStoreForm");
    		var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadStore();
    		}
    		alert(result2[1]);
    		form.reset();
    		
    	}
    	
    	function addPackageCallback(){
    		var form = document.forms("addPackageForm");
    		var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadPackage();
    		}
    		alert(result2[1]);
    		form.reset();
    	}
    	
    	function deleteStoreCallBack(){
    		var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadStore();
    		}
    		alert(result2[1]);
    	}
    	
    	function deletePackageCallBack(){
			var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadPackage();
    		}
    		alert(result2[1]);
		}
		
		function updatePackageOPCallback(){
			var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadPackage();
    		}
    		alert(result2[1]);
			var updatePackage = document.getElementById('updatePackageDiv');
			updatePackage.style.display = 'none';
		}
		
		function updateStoreOPCallback(){
			var result = arguments[0];
    		var result2 = result.split("||");
    		if (result2[0] == "1"){
    			loadStore();
    		}
    		alert(result2[1]);
			var updatePackage = document.getElementById('updateStoreDiv');
			updatePackage.style.display = 'none';
		}
		
		function update(obj,id){
		
			var updatePackage = document.getElementById('updatePackageDiv');
			
			updatePackage.style.left = document.body.clientWidth / 2 - 360/2;
				
			updatePackage.style.top = document.body.clientHeight / 2 - 200/2;
				
			updatePackage.style.display = 'block';
			var parent = obj.parentNode.parentNode;
			
			var tds = parent.getElementsByTagName("td");
			var form = document.forms('updatePackage');
			form.productName.value = tds[0].innerText;
			form.credits.value = tds[1].innerText;
			form.mpoints.value = tds[2].innerText;
			form.amount.value = tds[3].innerText;
			if (tds[4].innerText == "是"){
				form.isVIP.checked = true;
			}	
			form.id.value = id;
			//form.isVIP.value = tds[4].innerText;
			form.sort.value = tds[6].innerText;
			for (var j = 0; j<store.length;j++){
				if (tds[5].innerText == store[j][1]){
					if (form.accountId.options.length >= j){
						form.accountId.options[j].selected = true;	
						break;
					}
				}
			}
		}
		
		function updateStore2(obj,id){

			var updatePackage = document.getElementById('updateStoreDiv');

			updatePackage.style.left = document.body.clientWidth / 2 - 360/2;
				
			updatePackage.style.top = document.body.clientHeight / 2 - 200/2;

			updatePackage.style.display = 'block';
			var parent = obj.parentNode.parentNode;

			var tds = parent.getElementsByTagName("td");
			var form = document.forms('updateStore');
			form.productName.value = tds[0].innerText;
			form.password.value = tds[1].innerText;
			form.storeId.value = id;
			
		}
    	
    	function check_error(str){
				if (str.indexOf("errors") != -1)
					return true;
				return false;
		}
		
		function bindStore(){
			//检测仓库列表是否下载完毕
			if (standByStore == true && standByPackage == false){
				//检查是否已经发送了请求获取套餐列表
				if (sendRequest == false){
					loadPackage();
					sendRequest = true;
				}
			}
			
			//当套餐列表获取成功后，去除定时器动作
			if (timeId != null && standByPackage == true){
				clearInterval(timeId);
				timeId = null;
				sendRequest = false;
				standByStore = false;
				standByPackage = false;
			}
		}
		function closeDiv(divID){
			var div = document.getElementById(divID);
			div.style.display="none";
		}
		
	//]]>
    </script>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />
</head>

<body >
<div id="TabbedPanels2" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">新增套餐</li>
    <li class="TabbedPanelsTab" tabindex="0">新增仓库</li>
  </ul>
  <div class="TabbedPanelsContentGroup">
    <div class="TabbedPanelsContent">
    	<form name="addPackageForm" action="/mycndesign/addPackage.do" method="post">
        	<label for="productName">套餐名称：</label><input type="text" name="productName" />&nbsp; <label for="credits">Credit数量：</label><input type="text" name="credits" /><br/>
        	<label for="mpoints">M-Points：</label><input type="text" name="mpoints" /><label for="amount">价格：</label><input type="text" name="amount" /><br/>
        	<label for="isVIP">是否VIP套餐：</label><input type="radio" value="true" name="isVIP" />是 <input type="radio" value="false" name="isVIP" />否 <br/>
        	<label for="sort">排序</label><input type="text" size="3" name="sort"><br/>
        	<label for="accountId">绑定仓库：</label><select name="accountId" id="storeList"><option value="1">tangdesign</option></select><br/>
        	<input type="button" onClick="addPackage();" value="增加"> &nbsp;&nbsp; <input type="reset" value="重置">
        </form>
    </div>
    <div class="TabbedPanelsContent">
    	<form action="/mycndesign/addStore.do" name="addStoreForm" method="post">
    		<label for="storeName">仓库名称：</label><input type="text" name="storeName" />&nbsp;&nbsp;<label for="storePassword">仓库密码：</label><input name="storePassword" type="text"><br/>
    		<input type="button" onClick="addStore();" value="增加"> &nbsp;&nbsp; <input type="reset" value="重置">
    	</form>
    </div>
  </div>
</div>
<div style="padding:10px 10px">&nbsp;</div>
<div id="TabbedPanels1" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">套餐列表</li>
    <li class="TabbedPanelsTab" tabindex="0">仓库列表</li>
  </ul>
  <div class="TabbedPanelsContentGroup">
    <div class="TabbedPanelsContent" style="text-align:center;">
      <table width="95%" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" style="border:1px solid #56626d;font-size:12px">
      		<thead>
            <tr style="background-color:#46525d; color:#FFFFFF">
            	<th>套餐名称</th>
                <th>Credits</th>
                <th>M-Points</th>
                <th>价格</th>
                <th>是否VIP产品</th>
                <th>绑定仓库</th>
                <th>排序</th>
                <th>创建时间</th>
             	<th>操作</th>
            </tr>
            </thead>
            <tbody id ="packageData">
            	
            </tbody>
      </table>
    </div>
    <div class="TabbedPanelsContent" style="text-align:center;">
   	  <table width="70%" align="center" cellpadding="2" cellspacing="1" bgcolor="#FFFFFF" style="border:1px solid #56626d;font-size:12px">
      		<thead>
            <tr style="background-color:#46525d; color:#FFFFFF">
            	<th>仓库名称</th>
                <th>密码</th>
                <th>创建日期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="storeDate">
            
            </tbody>
      </table>
    </div>
  </div>
</div>


<script type="text/javascript">
<!--
var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
var TabbedPanels2 = new Spry.Widget.TabbedPanels("TabbedPanels2");
initPage();
//-->
</script>
<div class="test" id="updatePackageDiv" style="position:absolute;">
			<form method="POST" action="/mycndesign/updatePackage.do" name="updatePackage" >
				<table width="360" height="400">
				<input type="hidden" name="id"/>
				<tr style="border:1px solid #56626d;">
				<td align="right"><label for="productName">套餐名称：</label></td><td><input type="text" name="productName"></td>
				</tr>
				<tr>
					<td align="right"><label for="credits">Credits数量：</label></td>
					<td><input type="text" name="credits"></td>
				</tr>
				<tr>
					<td align="right"><label for="mpoints">M-Points：</label></td><td><input type="text" name="mpoints"></td>
				</tr>
                <tr>
					<td align="right"><label for="amount">价格：</label></td><td><input type="text" name="amount"></td>
				</tr>
				<tr>
					<td align="right"><label for="isVIP">是否VIP套餐：</label></td><td><input type="checkbox" name="isVIP"></td>
				</tr>
				<tr>
					<td align="right"><label for="sort">排序：</label></td><td><input name="sort" type="text" size="4"></td>
				</tr>
				<tr>
					<td align="right"><label for="sort">绑定仓库：</label></td><td><select name="accountId" id="storeList"><option value="1">tangdesign</option></select></td>
				</tr>
				<tr>
					<td align="center" colspan="2"><button onClick="updatePackageOP();" name="btnAction">修改</button>&nbsp;&nbsp;&nbsp;<button onClick="reset();" name="btnCancel">取消</button>&nbsp;&nbsp;&nbsp;<button onClick="closeDiv('updatePackageDiv');">关闭</button></td>
				</tr>
				</table>
			</form>
		
		</div>
<div class="test" id="updateStoreDiv" style="position:absolute;">
			<form method="POST" action="/mycndesign/updateStore.do" name="updateStore" >
				<table width="360" height="400">
				<input type="hidden" name="storeId"/>
				<tr style="border:1px solid #56626d;">
				<td align="right"><label for="productName">仓库名称：</label></td><td><input type="text" name="productName" disabled></td>
				</tr>
				<tr>
					<td align="right"><label for="password">密码：</label></td>
					<td><input type="text" name="password"></td>
				</tr>
				<tr>
					<td align="right"><label for="password">确认密码：</label></td>
					<td><input type="text" name="com_password"></td>
				</tr>
                <tr>
					<td align="center" colspan="2"><button onClick="updateStoreOP();" name="btnAction">修改</button>&nbsp;&nbsp;&nbsp;<button onClick="reset();" name="btnCancel">取消</button>&nbsp;&nbsp;&nbsp;<button onClick="closeDiv('updateStoreDiv');">关闭</button></td>
				</tr>
				</table>
			</form>
		
		</div>
	</body>
</html:html>
